package com.java.basis.date;

import org.junit.Test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 测试 Date
 *
 * @author wangning
 * @create 2021-04-15 11:23
 */
public class TestDate01 {

	/**
	 * 获取时间戳
	 */
	@Test
	public void getTimeStamp() {

		Date date = new Date();
		System.out.println("date = " + date);
		System.out.println("获取时间戳： " + date.getTime());
		System.out.println("获取时间戳： " + new Date().getTime());

		Date date1 = new Date(2020, 2, 28);//此方法不建议使用
		System.out.println("date1 = " + date1);

	}

	/**
	 * 格式化时间
	 */
	@Test
	public void formatDate() {
		long times = new Date().getTime();
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		String formatDate = simpleDateFormat.format(times);
		System.out.println("formatDate = " + formatDate);
	}

	/**
	 * 时间戳转化为标准日期
	 */
	@Test
	public void timestampToStandardDate() {
		long times = 1618473631077L;
		Date date = new Date(times);
		System.out.println("date = " + date);
	}

	/**
	 *
	 */
	@Test
	public void otherMehtodGetDate() {
		Date currentTime = new Date();
//		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		System.out.println("dateString = " + dateString);
		//可以根据不同格式的日期转化为标准格式的日期
		ParsePosition pos = new ParsePosition(0);//用来标明解析开始位，其实也可以不明传pos的
		Date currentTime_2 = formatter.parse(dateString, pos);
		System.out.println("currentTime_2 = " + currentTime_2);

	}

	/**
	 * 获取一个月得最后一天
	 */
	private static String firstDay;
	private static String lastDay;

	@Test
	public void getLastDayOfMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		//获取前月的第一天
		Calendar cal_1 = Calendar.getInstance();//获取当前日期
		cal_1.add(Calendar.MONTH, -1);
		cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
		firstDay = format.format(cal_1.getTime());
		System.out.println("-----1------firstDay:" + firstDay);
		//获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 0);//设置为0,当前日期既为上一个月的最后一天
		lastDay = format.format(cale.getTime());
		System.out.println("-----2------lastDay:" + lastDay);
		//获取当前月第一天：
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
		String first = format.format(c.getTime());
		System.out.println("===============first:" + first);
		//获取当前月最后一天
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		String last = format.format(ca.getTime());
		System.out.println("===============last:" + last);

	}

	/**
	 * 根据用户传入的格式就行格式化
	 */
	@Test
	public void formatDateAccordingTo() {
		String yyyyMMdd = getUserDate("yyyyMMdd");
		System.out.println("yyyyMMdd = " + yyyyMMdd);
	}

	public static String getUserDate(String format) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 计算两个日期之间的天数
	 */
	@Test
	public void days() {
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date star = dft.parse("2020-03-02");//开始时间
			Date endDay = dft.parse("2020-04-02");//结束时间
			Long starTime = star.getTime();
			Long endTime = endDay.getTime();
			Long num = endTime - starTime;//时间戳相差的毫秒数
			System.out.println("相差天数为：" + num / 24 / 60 / 60 / 1000);//除以一天的毫秒数
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}


}
